# $NetBSD: Makefile,v 1.19 2023/04/19 08:10:36 adam Exp $

.include "../../wip/llvm/version.mk"

DISTNAME=	libcxxabi-${LLVM_VERSION}.src
PKGNAME=	${DISTNAME:S/.src//:S/libcxxabi-/wasi-libcxx-/}
CATEGORIES=	lang devel

MAINTAINER=	pkgsrc-users@NetBSD.org
HOMEPAGE=	https://libcxxabi.llvm.org/
COMMENT=	C++ Standard Librares (libc++/libc++abi) for WASI/WebAssembly
LICENSE=	apache-2.0

WRKSRC=		${WRKDIR}

# libcxx requires libcxxabi's cxxabi.h to build, but libcxxabi requires
# libcxx's includes to build, so we have this unholy extra distfiles thing
# in both of them to get them to build against each other without causing
# circular dependencies.  This must be kept in sync with libcxxabi/Makefile.
LIBCXX=		libcxx-${LLVM_VERSION}.src
EXTRA_DIST+=	${LIBCXX}${EXTRACT_SUFX}
SITES.${LIBCXX}${EXTRACT_SUFX}=	\
	${MASTER_SITES:=${GITHUB_PROJECT}/releases/download/${GITHUB_RELEASE}/}

TOOL_DEPENDS+=	lld-[0-9]*:../../wip/lld

# Workaround for header conflicts
BUILDLINK_TRANSFORM+=	rm:-I/usr/include
BUILDLINK_TRANSFORM+=	rm:-I${PREFIX}/include

BUILD_DIRS+=		build_libcxx
BUILD_DIRS+=		build_libcxxabi
INSTALL_DIRS+=		${BUILD_DIRS}

USE_LANGUAGES=	c c++
USE_CMAKE=	yes

SSP_SUPPORTED=		no
PKGSRC_USE_STACK_CHECK=	no

CMAKE_ARGS+=	-DCMAKE_BUILD_TYPE=Release
CMAKE_ARGS+=	-DLIBCXXABI_LIBCXX_INCLUDES=${WRKDIR}/libcxx/include
CMAKE_ARGS+=	-DLIBCXXABI_USE_LLVM_UNWINDER=OFF
CMAKE_ARGS+=	-DCMAKE_C_COMPILER_WORKS=1
CMAKE_ARGS+=	-DCMAKE_CXX_COMPILER_WORKS=1
CMAKE_ARGS+=	-DCXX_SUPPORTS_CXX11=ON
CMAKE_ARGS+=	-DLLVM_COMPILER_CHECKED=ON
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L=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WNEWLINE_EOF_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WUNUSED_FUNCTION_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSHADOW_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSHORTEN_64_TO_32_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSIGN_COMPARE_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSIGN_CONVERSION_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSTRICT_ALIASING_EQ_2_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WSTRICT_OVERFLOW_EQ_4_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WUNUSED_PARAMETER_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_WUNUSED_VARIABLE_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_PEDANTIC_FLAG:BOOL=ON
CMAKE_ARGS+=	-DLIBCXXABI_SUPPORTS_FSTRICT_ALIASING_FLAG:BOOL=ON
CMAKE_ARGS+=	-DCMAKE_CXX_LINK_FLAGS=${LDFLAGS:Q}
CMAKE_ARGS+=	-DCMAKE_C_FLAGS=-fno-exceptions
CMAKE_ARGS+=	-DCMAKE_CXX_FLAGS=-fno-exceptions
CMAKE_ARGS+=	-DLIBCXX_CXX_ABI=system-libcxxabi
CMAKE_ARGS+=	-DLIBCXX_CXX_ABI_INCLUDE_PATHS=${WRKDIR}/libcxxabi/include

CMAKE_INSTALL_PREFIX=	${PREFIX}/wasi

# avoid conflict with stddef.h
CPPFLAGS.NetBSD+=	-D__DEFINED_max_align_t=1

post-extract:
	${MKDIR} ${WRKDIR}/build_libcxx
	${MKDIR} ${WRKDIR}/build_libcxxabi
	${LN} -f -s ${WRKDIR}/${DISTNAME} ${WRKDIR}/libcxxabi
	${LN} -f -s ${WRKDIR}/${LIBCXX} ${WRKDIR}/libcxx

do-configure:
	cd ${WRKSRC} && cd build_libcxx && \
	${PKGSRC_SETENV} ${_CONFIGURE_CMAKE_ENV} \
		cmake ${CMAKE_ARGS} ../libcxx
	cd ${WRKSRC} && cd build_libcxxabi && \
	${PKGSRC_SETENV} ${_CONFIGURE_CMAKE_ENV} \
		cmake ${CMAKE_ARGS} ../libcxxabi

# Run llvm-ranlib again on the archives. Fixes mystery build failure
# in Firefox due to missing index (see PR pkg/56590). Might be a MAKE_JOBS
# problem in this package?
post-install:
	${PREFIX}/bin/llvm-ranlib \
	  ${DESTDIR}${PREFIX}/wasi/lib/wasm32-wasi/libc++.a
	${PREFIX}/bin/llvm-ranlib \
	  ${DESTDIR}${PREFIX}/wasi/lib/wasm32-wasi/libc++abi.a

.include "../../wip/llvm/buildlink3.mk"
.include "../../wip/wasi-libc/buildlink3.mk"
.include "../../wip/wasi-compiler-rt/buildlink3.mk"
ABI=			# 32
CFLAGS=			-O2
CXXFLAGS=		-O2
PKGSRC_COMPILER=	clang
CLANGBASE=		${PREFIX}
BUILDLINK_DEPMETHOD.clang=	build
.include "../../wip/clang/buildlink3.mk"
.include "../../mk/bsd.pkg.mk"
